// TargetShooter.cpp //
#include "TargetShooter.h"

#include "Player.h"
#include "Bullet.h"

//bad guy that aims at you

TargetShooter::TargetShooter(string keyname) : Enemy(keyname)
{
	pointValue = TargetShooterPointValue;
	health = TargetShooterHealth;
	speed = TargetShooterSpeed;
	target = NULL;
	targetDirection = Vector(0, 0);
	lastFired = 0.0f;
}

void TargetShooter::Update(float deltaTime)
{
	if(this->numValue == -1)
		this->TakeDamage(this->health);


    if(target == NULL || target->dead) // choose target
		target = PickRandomObject(&PlayerShip::PlayerShipList);

	if(target != NULL) // aim
	{
		targetDirection = target->position - position;
	    targetDirection.Normalize();
	}
	else
	    targetDirection = Vector(0, 1); // straight down if player dead
	    
	Enemy::Update(deltaTime);

	lastFired += deltaTime;
	if(lastFired > TargetShooterFireDelay)
	    FireBullet();
}

void TargetShooter::FireBullet()//fires the target bullet
{
	Bullet* bullet = new Bullet("Glow");
	bullet->health = 20;
    bullet->position = position;
    bullet->direction = targetDirection;
    bullet->CollisionList = &PlayerShip::PlayerShipList;

    lastFired = 0.0f;
}

//renders with white text
void TargetShooter::Render(SDL_Surface* screen)
{
	char buffer[4];
	sprintf(buffer, "%d", this->numValue);
	sprite->Render(screen,position);
	sprite->texture->DrawText(screen,buffer,20,this->position.X,this->position.Y,0,0,0,true);
}

